package com.lancoo.edu.platform.operation.commons.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.net.URL;
import java.util.regex.Matcher;

public class UrlUtil {

    private final static Logger log = LoggerFactory.getLogger(UrlUtil.class);

    // 正则化文件路径
    public static String normalizePath(String path) {
        String result = path.replaceAll("/+", Matcher.quoteReplacement(File.separator));
        return result.replaceAll("\\\\+", Matcher.quoteReplacement(File.separator));
    }

    /**
     * 正则化URL路径
     *
     * @param path 规范化路径
     * @return 规范化之后的路径
     */
    public static String normalizeUrl(String path) {
        try {
            URL uri = new URL(path);
            return uri.toString();
        } catch (Exception exception) {
            log.error("地址有误", exception);
        }
        return "";
    }
}
